package com.DAO.Impl;

import com.DAO.ClassDAO;
import com.DBUtil.DBUtil;
import com.entity.Class;
import com.google.gson.Gson;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 20x
 * @date 2024 - 11 -27
 **/
public class ClassDAOImpl implements ClassDAO {
    @Override
    public List<Class> selectNotReady(String Type, String week) {
        List<Class> list = new ArrayList<>();
        String sql = "select * from classAll where readyStatus='准备中'";
        if(!Type.equals("")){
            sql += " and classType = '"+Type+"'";
        }
        if(!week.equals("")){
            sql += " and weekOfMonth = '"+week+"'";
        }
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement(sql);
            ResultSet rst = pst.executeQuery();
            while (rst.next()){
                int classId  = rst.getInt(1);
                String weekOfMonth = rst.getString(2);
                String weekDay = rst.getString(3);
                String time = rst.getString(4);
                String classType = rst.getString(5);
                String classStuatus = rst.getString(6);
                String firstClass = rst.getString(7);
                String secondClass = rst.getString(8);
                String thirdClass = rst.getString(9);
                String fourthClass = rst.getString(10);
                Class aClass = new Class(classId, weekOfMonth, weekDay, time, classType,classStuatus, firstClass, secondClass, thirdClass, fourthClass);
                list.add(aClass);
            }
            DBUtil.close(rst,pst,conn);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public List<Class> select(String Type,String week) {
        List<Class> list = new ArrayList<>();
        String sql = "select * from classAll where readyStatus='已添加'";
        if(!Type.equals("")){
            sql += " and classType = '"+Type+"'";
        }
        if(!week.equals("")){
            sql += " and weekOfMonth = '"+week+"'";
        }
        sql +="  ORDER BY weekOfMonth DESC";
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement(sql);
            System.out.println(sql);
            ResultSet rst = pst.executeQuery();
            while (rst.next()){
                 int classId  = rst.getInt(1);
                 String weekOfMonth = rst.getString(2);
                 String weekDay = rst.getString(3);
                 String time = rst.getString(4);
                 String classType = rst.getString(5);
                 String classStuatus = rst.getString(6);
                 String firstClass = rst.getString(7);
                 String secondClass = rst.getString(8);
                 String thirdClass = rst.getString(9);
                 String fourthClass = rst.getString(10);
                Class aClass = new Class(classId, weekOfMonth, weekDay, time, classType,classStuatus, firstClass, secondClass, thirdClass, fourthClass);
                list.add(aClass);
            }
            DBUtil.close(rst,pst,conn);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public int updataClass(String id, String index, String ClassType, String site, String teacher) {
        int count = 0;
        String sql = "UPDATE classAll SET ";
        if ("AM 8.00~9.00".equals(index)) {
            sql += "firstClass = ? ";
        } else if ("AM 9.30~10.30".equals(index)) {
            sql += "secondClass = ? ";
        } else if ("PM 14.00~15.00".equals(index)) {
            sql += "thirdClass = ? ";
        } else {
            sql += "fourthClass = ? ";
        }
        sql += "WHERE readyId = ?";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pst = conn.prepareStatement(sql)) {
            // 创建JSON字符串
            String jsonValue = String.format("{\"class\":\"%s\",\"site\":\"%s\",\"teacher\":\"%s\"}", ClassType, site, teacher);

            // 设置参数
            pst.setString(1, jsonValue);
            pst.setString(2, id);
            // 执行更新
            count = pst.executeUpdate();
            DBUtil.close(null,pst,conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count;
    }

    @Override
    public List<Class> ListClassReady(String Type) {
        List<Class> list = new ArrayList<>();
        String sql = "select * from classAll where  readyStatus='准备中' ";
        if(!Type.equals("")){
            sql += " and classType = '"+Type+"'";
        }
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement(sql);
            System.out.println(sql);
            ResultSet rst = pst.executeQuery();
            while (rst.next()){
                int classId  = rst.getInt(1);
                String weekOfMonth = rst.getString(2);
                String weekDay = rst.getString(3);
                String time = rst.getString(4);
                String classType = rst.getString(5);
                String classStatus = rst.getString(6);
                String firstClass = rst.getString(7);
                String secondClass = rst.getString(8);
                String thirdClass = rst.getString(9);
                String fourthClass = rst.getString(10);
                Class aClass = new Class(classId, weekOfMonth, weekDay, time, classType,classStatus, firstClass, secondClass, thirdClass, fourthClass);
                list.add(aClass);
            }
            DBUtil.close(rst,pst,conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public int UpdataClassStatus(String weekOfMonth) {
        int count = 0;
        String sql = "update classAll set readyStatus = '已添加' where weekOfMonth = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pst = conn.prepareStatement(sql)) {
            pst.setString(1, weekOfMonth);
            count = pst.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count;
    }

    @Override
    public int Inesertcoaches(String weekOfMonth,String  weekDay, String time, String ClassType) {
        int count = 0;
        PreparedStatement pst = null;
        Connection conn  = DBUtil.getConnection();
        String sql = "INSERT INTO classAll (weekOfMonth, weekDay, time, ClassType, readyStatus, firstClass, secondClass, thirdClass, fourthClass) VALUES" +
                "('"+weekOfMonth+"', '"+weekDay+"', '"+time+"', '"+ClassType+"', '准备中'," +
                " '{\"class\":\"\",\"site\":\"\",\"teacher\":\"\"}', " +
                " '{\"class\":\"\",\"site\":\"\",\"teacher\":\"\"}', " +
                " '{\"class\":\"\",\"site\":\"\",\"teacher\":\"\"}', " +
                " '{\"class\":\"\",\"site\":\"\",\"teacher\":\"\"}')";
        try  {
            pst = conn.prepareStatement(sql);
            count = pst.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count;
    }

    @Override
    public String slelectNewData() {
        String weekOfMonth = null;
        PreparedStatement pst = null;
        Connection conn  = DBUtil.getConnection();
        ResultSet rs = null;
        String sql = "select top 1 time from classAll  ORDER BY time DESC";
        try{
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()){
                weekOfMonth = rs.getString("time");
            }
        }
        catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return weekOfMonth;
    }


    public static void main(String[] args) {
        ClassDAOImpl c = new ClassDAOImpl();
//        List<Class> list = c.select("","");
//        System.out.println(list.toString());

//        System.out.println(c.updataClass("1","AM 8.00~9.00","足球","足球场","张三"));

        System.out.println(new ClassDAOImpl().slelectNewData());

    }
}
